/*
 *    This file is part of BlobDB.
 *
 *    BlobDB is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    BlobDB is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with BlobDB.  If not, see <http://www.gnu.org/licenses/>.
 *    
 *    Copyright(c) 2011 Michael Kankkonen
 *    http://www.vnetcon.org
 */


package org.vnetcon.blobdb.database.backend.memory;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;

import org.vnetcon.blobdb.database.SQLTransactionException;
import org.vnetcon.blobdb.database.backend.QueryInterface;
import org.vnetcon.blobdb.database.backend.memory.mdo.PMF;
import org.vnetcon.blobdb.database.backend.memory.mdo.Table;
import org.vnetcon.blobdb.database.backend.memory.mdo.TableData;
import org.vnetcon.blobdb.database.backend.memory.mdo.TableID;

public class MemoryQuery implements QueryInterface {

	private String filter = null;
	private String order = null;
	private String params = null;
	int start = 0;
	int end = 1000;
	MemoryTransaction trans = null;

	private int SEARCH_FREE_SPACE = 1;
	private int SEARCH_FOR_UPDATE = 2;
	private int search_type = SEARCH_FREE_SPACE;
	
	public MemoryQuery(MemoryTransaction trans){
		this.trans = trans;
	}
	
	@Override
	public List<?> execute() {
		ArrayList<MemoryTableID> aRet = new ArrayList<MemoryTableID>();
		Enumeration<Integer> e = PMF.hTableID.keys();
		while (e.hasMoreElements()) {
			int tiid = e.nextElement();
			TableID ti = PMF.hTableID.get(tiid);
			MemoryTableID mti = new MemoryTableID(ti);
			aRet.add(mti);
		}
		return aRet;
	}

	@Override
	public void setFilter(String filter) {

		if(filter.indexOf("tableDataIDParam") > -1){
			this.search_type = this.SEARCH_FOR_UPDATE;
		}else{
			this.search_type = this.SEARCH_FREE_SPACE;
		}
		this.filter = filter;
	}

	@Override
	public void declareParameters(String params) {
		this.params = params;
	}

	@Override
	public List<?> execute(Object tableId) {
		ArrayList<MemoryTableData> aRet = new ArrayList<MemoryTableData>();
		Enumeration<Integer> e = PMF.hTableData.keys();
		while(e.hasMoreElements()){
			int tiid = e.nextElement();
			TableData ti = PMF.hTableData.get(tiid);

			if(this.search_type == this.SEARCH_FREE_SPACE){
				if (ti.getFreeRows() > 0 && ti.getTableID() == (Integer) tableId) {
					MemoryTableData mti = new MemoryTableData(ti);
					aRet.add(mti);
				}
			}

			if(this.search_type == this.SEARCH_FOR_UPDATE){
				if (ti.getTableDataID() == (Integer) tableId) {
					MemoryTableData mti = new MemoryTableData(ti);
					aRet.add(mti);
				}
			}
			
		}

		Collections.sort(aRet);
		return aRet;
	}

	@Override
	public List<?> execute(Object tableName, Object schema) {
		ArrayList<MemoryTable> aRet = new ArrayList<MemoryTable>();
		Enumeration<Integer> e = PMF.hTable.keys();
		while(e.hasMoreElements()){
			int tiid = e.nextElement();
			Table ti = PMF.hTable.get(tiid);
			if(ti.getTableName().equals(tableName) && ti.getSchema().equals(schema)){
				MemoryTable mti = new MemoryTable(ti);
				aRet.add(mti);
				if(this.trans.isTransactionBegin()){
					this.trans.addToTransaction(ti);
				}
			}
		}
		return aRet;
	}

	@Override
	public void setOrdering(String order) {
		this.order = order;
	}

	@Override
	public void setRange(int start, int end) {
		this.start = start;
		this.end = end;
	}

}
